<?php

class SourceBB_Process_phpBB
{
	public function __construct($sbb_path="") {

		global $phpbb_root_path, $phpEx, $user, $db, $config, $cache, $template;

		// Basic setup
		include($sbb_path . 'settings.php');
		include($sbb_path . 'classes/db.php');

		// Get phpBB constants
		define('IN_PHPBB', true);
		$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : "$phpbbpath";
	
		$phpEx = substr(strrchr(__FILE__, '.'), 1);
		include_once($phpbb_root_path . 'common.' . $phpEx);
	}

	public function __destruct() {
		
	}

	public function updateForumID($forumid) {
		global $phpbb_groups_enabled;
	
		// Cancel if phpBB groups integration is disabled
		if (!$phpbb_groups_enabled) return;
		
		// Cancel if $forumid is not an integer		
		if (!is_numeric($forumid)) return;

		// Find out the internal SourceBB ID for the user
		$pre_query = mysql_query("SELECT * FROM sm_admins_pre WHERE forumid = '" . $forumid . "';");
		if (mysql_num_rows($pre_query) != 1) return;
		$pre_result = mysql_fetch_assoc($pre_query);
		$sbbid = $pre_result['id'];
		
		// Run the user update function
		$this->updateUser($sbbid,$forumid);
	}

	public function updateSBBID($sbbid) {
		global $phpbb_groups_enabled;

		// Cancel if phpBB groups integration is disabled
		if (!$phpbb_groups_enabled) return;
	
		// Cancel if $sbbid is not an integer
		if (!is_numeric($sbbid)) return;

		// Find out what the user's forum ID is
		$pre_query = mysql_query("SELECT * FROM sm_admins_pre WHERE id = '" . $sbbid . "';");
		if (mysql_num_rows($pre_query) != 1) return;
		$pre_result = mysql_fetch_assoc($pre_query);
		$forumid = $pre_result['forumid'];
		
		// Run the user update function
		$this->updateUser($sbbid,$forumid);
	}

	private function updateUser($sbbid,$forumid) {
		global $phpbbdb;
		
		// Check to see if the user has a verified Steam ID. If not, don't bother continuing.
		$smadmin_query = mysql_query("SELECT * FROM sm_admins WHERE id = '" . $sbbid . "';");
		if (mysql_num_rows($smadmin_query) != 1) return;

		// Assume no permissions before processing the phpBB groups
		$userflags = "";
		$userimm = 0;

		// This query returns the flags and immunity level for any phpBB group that the user is in that has an entry in sm_groups_phpbb
		$permgroups_query_text = "SELECT sm_groups_phpbb.flags, sm_groups_phpbb.immunity_level FROM sm_groups_phpbb INNER JOIN $phpbbdb." . USER_GROUP_TABLE . " ON sm_groups_phpbb.id = " . USER_GROUP_TABLE . ".group_id WHERE " . USER_GROUP_TABLE . ".user_id = '" . $forumid . "';";
		$permgroups_query = mysql_query($permgroups_query_text) or die(mysql_error());

		// Add any permissions from the groups
		while($permgroup = mysql_fetch_assoc($permgroups_query))
		{
			$userflags .= $permgroup['flags'];
			if ($permgroup['immunity_level'] > $userimm) $userimm=$permgroup['immunity_level'];
		}
		
		// Add any flags that have been set directly on the user in SourceBB
		$smpre_query = mysql_query("SELECT * FROM sm_admins_pre WHERE id = '" . $sbbid . "';");
		$smpre_result = mysql_fetch_assoc($smpre_query);
		
		$userflags .= $smpre_result['user_flags'];
		if ($smpre_result['user_immunity'] > $userimm) $userimm=$smpre_result['user_immunity'];

		// Clean up the flags (sort and remove duplicates)
		$userflags = $this->cleanFlags($userflags);

		// Write the permissions back to the database
		$update_query_text = "UPDATE sm_admins SET flags='" . $userflags . "', immunity='" . $userimm . "' WHERE id='" . $sbbid . "';";
		$update_query = mysql_query($update_query_text);
	}

	private function cleanFlags($flags)
	{
		$str_arr = str_split($flags);
		$str_arr = array_unique($str_arr, SORT_STRING);
		sort($str_arr, SORT_STRING);
		$flags = implode("",$str_arr);
		return($flags);
	}

}

?>